﻿Imports System.Collections.Generic
Imports System.Data
Imports Biz
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos para el manejo de la entidad Medios_Busqueda
''' </summary>
''' <remarks></remarks>
Public Class DALMedios_Busqueda

    Private Shared m_Instance As DALMedios_Busqueda
    Private Shared m_Semaforo As New Object
    Private m_TDGMedios_Busqueda As TDGMedios_Busqueda


    Public Function Insert(obj As cMedios_Busqueda) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            iResultado = m_TDGMedios_Busqueda.Insert(database.Conexion, _
                                                    obj.MedB_Descrip, _
                                                    obj.Tipo_Medio_Busqueda.TMedB_Codigo, _
                                                    obj.MedB_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.Medios_BusquedaFalloAlta(ex)
        End Try

    End Function

    Public Function Update(obj As cMedios_Busqueda) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            m_TDGMedios_Busqueda.Update(database.Conexion, _
                                                    obj.MedB_Codigo, _
                                                    obj.MedB_Descrip, _
                                                    obj.Tipo_Medio_Busqueda.TMedB_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.Medios_BusquedaFalloEditar(ex)
        End Try
    End Function

    Public Function Delete(obj As cMedios_Busqueda) As Boolean
        Dim iResultado As Int16 = 0
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            m_TDGMedios_Busqueda.Delete(database.Conexion, obj.MedB_Codigo)
            database.Desconectar()

            Return (iResultado > 0)
        Catch ex As Exception
            Throw New Excepciones.Medios_BusquedaFalloBaja(ex)
        End Try

    End Function

    Public Function Sel(obj As cMedios_Busqueda) As cMedios_Busqueda
        Dim dr As SqlDataReader
        Dim database As cDatabase

        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGMedios_Busqueda.Find(database.Conexion, obj.MedB_Codigo)

            If dr.HasRows Then

                dr.Read()

                obj.MedB_Codigo = Utils.Convert.ToInt16(dr.GetValue(0))
                obj.MedB_Descrip = Utils.Convert.ToStr(dr.GetValue(1))
                obj.Tipo_Medio_Busqueda.TMedB_Codigo = Utils.Convert.ToInt16(dr.GetValue(2))
                obj.Tipo_Medio_Busqueda.TMedB_Descrip = Utils.Convert.ToStr(dr.GetValue(4))
                obj.DVH = Utils.Convert.ToInt32(dr.GetValue(3))

                dr.Close()
            Else
                obj = Nothing
            End If

            database.Desconectar()

            Return obj
        Catch ex As Exception
            Throw New Excepciones.Medios_BusquedaFalloBuscar(ex)
        End Try
    End Function

    Public Function SelectAll() As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cMedios_Busqueda
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGMedios_Busqueda.FindAll(database.Conexion)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cMedios_Busqueda

                    objAux.MedB_Codigo = Utils.Convert.ToInt16(dr.GetValue(0))
                    objAux.MedB_Descrip = Utils.Convert.ToStr(dr.GetValue(1))
                    objAux.Tipo_Medio_Busqueda.TMedB_Codigo = Utils.Convert.ToInt16(dr.GetValue(2))
                    objAux.DVH = Utils.Convert.ToInt32(dr.GetValue(3))
                    objAux.Tipo_Medio_Busqueda.TMedB_Descrip = Utils.Convert.ToStr(dr.GetValue(4))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.Medios_BusquedaFalloConsultar(ex)
        End Try
    End Function

    Public Function SelectPorTipo_All(iTMedB_Codigo As Int16) As Collection
        Dim dr As SqlDataReader
        Dim colObjetos As Collection
        Dim objAux As cMedios_Busqueda
        Dim database As cDatabase


        Try
            database = cDatabase.Instance

            database.Conectar()
            dr = m_TDGMedios_Busqueda.FindPorTipo_All(database.Conexion, iTMedB_Codigo)

            If dr.HasRows Then

                colObjetos = New Collection

                Do While dr.Read

                    objAux = New cMedios_Busqueda

                    objAux.MedB_Codigo = Utils.Convert.ToInt16(dr.GetValue(0))
                    objAux.MedB_Descrip = Utils.Convert.ToStr(dr.GetValue(1))
                    objAux.Tipo_Medio_Busqueda.TMedB_Codigo = Utils.Convert.ToInt16(dr.GetValue(2))
                    objAux.DVH = Utils.Convert.ToInt32(dr.GetValue(3))
                    objAux.Tipo_Medio_Busqueda.TMedB_Descrip = Utils.Convert.ToStr(dr.GetValue(4))

                    colObjetos.Add(objAux)

                Loop

                dr.Close()

            Else

                colObjetos = Nothing

            End If

            database.Desconectar()

            Return colObjetos
        Catch ex As Exception
            Throw New Excepciones.Medios_BusquedaFalloConsultar(ex)
        End Try
    End Function

    Public Shared Function Instance() As DALMedios_Busqueda
        If m_Instance Is Nothing Then
            SyncLock (m_Semaforo)
                If m_Instance Is Nothing Then
                    m_Instance = New DALMedios_Busqueda
                End If
            End SyncLock
        End If

        Return m_Instance
    End Function

    Private Sub New()
        m_TDGMedios_Busqueda = TDGMedios_Busqueda.Instance()
    End Sub

End Class
